#ifndef SIMSRV_H
#define SIMSRV_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMap>

typedef QMap<QString, QString> StringMap;

class SimpleServer : public QWidget
{
    Q_OBJECT

private:
    typedef QMap<QTcpSocket*, QString> SocketStringMap;

private:
    QTcpServer *srv;
    quint16 pport;
//    bool pconnected;
    SocketStringMap Buffers;

    void parseCommand(QTcpSocket *socket, QString txt);

private slots:
    void onClientConnect();
    void onClientDisconnect();
    void onClientRead();

public:
    explicit SimpleServer(QWidget *parent = 0);
    ~SimpleServer();

    QTcpServer* server() {return srv;}

    static QString getTextBefore(QString pat, QString &txt);

    quint16 port() {return pport;}
    void setPort(quint16 port);

    bool active() {return srv->isListening();}
    void setActive(bool active);
    bool connected() {return Buffers.count();}
    int clientCount() {return Buffers.count();}

    void sendCommandTo(QTcpSocket *socket, QString cmd, StringMap array);

signals:
    void clientConnect(QTcpSocket *socket);
    void clientDisconnect(QTcpSocket *socket);
    void clientReceiveCommand(QTcpSocket *socket, QString cmd, StringMap array);

public slots:

};

#endif // SIMSRV_H
